﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CE.Database;
using System.Data;
using CE.Global;
using System.Collections;

public partial class carinsurance_ViewAgentCallStatus : System.Web.UI.Page
{
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "201")
        {
            Response.Redirect("~/carinsurance/PendingApplication.aspx");
        }

        if (!Page.IsPostBack)
        {
            ddlOrderBy.SelectedIndex = 0;
            LoadAgentList();
            ddlAgentList.Text = Session[General.UserId].ToString();
          
            
            RefreshGridView();

            DateTime tmpStartDate = dateTimeNow;
            DateTime tmpEndDate = dateTimeNow;

            if (Request.Params["viewdate"] != null)
            {
                string _strDate = Request.Params["viewdate"].ToString();
                string _strYear = _strDate.Substring(0, 4);
                string _strMonth = _strDate.Substring(4, 2);
                string _strDay = _strDate.Substring(6, 2);

                tmpStartDate = new DateTime(Convert.ToInt32(_strYear), Convert.ToInt32(_strMonth), Convert.ToInt32(_strDay));
                tmpEndDate = tmpStartDate.AddDays(7);
                tmpStartDate = tmpStartDate.AddDays(-7);

                while (tmpEndDate.CompareTo(tmpStartDate) >= 0)
                {
                    ddlViewDate.Items.Add(new ListItem(tmpStartDate.ToString("dd/MMM/yyyy"), tmpStartDate.ToString("yyyyMMdd")));
                    tmpStartDate = tmpStartDate.AddDays(1);
                }

                ddlViewDate.Text = Request.Params["viewdate"].ToString();
            }
            else
            {
                tmpStartDate = dateTimeNow.AddDays(-80);
                tmpEndDate = dateTimeNow.AddDays(-70);

                while (tmpEndDate.CompareTo(tmpStartDate) >= 0)
                {
                    ddlViewDate.Items.Add(new ListItem(tmpStartDate.ToString("dd/MMM/yyyy"), tmpStartDate.ToString("yyyyMMdd")));
                    tmpStartDate = tmpStartDate.AddDays(1);
                }
            }

            ddlViewTime.Items.Add(new ListItem("All Time", "0"));
            for (int i = 9; i < 19; i++)
            {
                tmpStartDate = new DateTime(tmpStartDate.Year, tmpStartDate.Month, tmpStartDate.Day, i, 0, 0);
                ddlViewTime.Items.Add(new ListItem(tmpStartDate.ToString("hh:00:ss tt") + " to " + tmpStartDate.AddHours(1).ToString("hh:mm:ss tt"), i.ToString()));
            }
            if (Request.Params["viewtime"] != null)
            {
                ddlViewTime.Text = Request.Params["viewtime"].ToString();
            }
           
        }

        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "100"
            && Session[CE.Global.General.UserId].ToString() != "1499") //Broker 2 is Supervisor
        {
            ddlAgentList.Enabled = false;
        }

        GridView1.DataBind();
        lblRecordCount.Text = "(" + GridView1.Rows.Count.ToString() + ") record(s). " + Resources.General.DateTime + ": " + dateTimeNow.ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
    }

    private void LoadAgentList()
    {
        try
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblList");
            string _spName = "sprocCXCRMGetAgentList";

            SortedList paraList = new SortedList();
            paraList.Add("@AgentID", Session[General.UserId].ToString());
            _dt = _db.GetDataTableBySP(_spName, paraList);

            ddlAgentList.Items.Clear();
            ddlAgentList.Items.Add(new ListItem("ALL", "0"));
            if (_dt != null && _dt.Rows.Count > 0)
            {
                
                for (int i = 0; i < _dt.Rows.Count; i++)
                {
                    ddlAgentList.Items.Add(new ListItem(_dt.Rows[i]["FULLNAME"].ToString(), _dt.Rows[i]["CEID"].ToString()));
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            throw;
        }
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        RefreshGridView();
    }

    private void RefreshGridView()
    {
        GridView1.DataBind();
        if (GridView1.Rows.Count > 0)
        {
            btnPrint.Visible = true;
            lblRecordCount.Text = "(" + GridView1.Rows.Count.ToString() + ") record(s). " + Resources.General.DateTime + ": " + dateTimeNow.ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
        else
        {
            btnPrint.Visible = false;
            lblRecordCount.Text = "(" + GridView1.Rows.Count.ToString() + ") record(s). " + Resources.General.DateTime + ": " + dateTimeNow.ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
    }

    protected void ddlOrderBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnRefresh_Click(null, null);
    }

    private Int32 GetOverDueCount()
    {
        Int32 _result = 0;
        MyDatabase _myDb = new MyDatabase();
        DataTable _dtOverDue = new DataTable("tblOverDue");
        _dtOverDue = _myDb.GetDataTable(@"SELECT COUNT(*) RecordCount FROM tblCXCRMCallStatus WHERE ((CONVERT(VARCHAR(8), ModifiedDate+1, 112) >= '20120801' AND ModifiedDate+1 <= dbo.fn_getUTCDateTime() AND CallFinishType = 3 AND AgentId = " + ddlAgentList.Text + ") OR (CONVERT(VARCHAR(8), CallBackDateTime, 112) >= '20120801' AND CallBackDateTime <= dbo.fn_getUTCDateTime() AND CallFinishType = 2 AND CallBackAgentId = " + ddlAgentList.Text + "))");

        if (_dtOverDue != null && _dtOverDue.Rows.Count > 0)
        {
            _result = Convert.ToInt32(_dtOverDue.Rows[0]["RecordCount"].ToString());
        }
        return _result;
    }

    

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hplCallNow = (HyperLink)e.Row.FindControl("hplCallNow");
            Literal lblCallTypeDesc = (Literal)e.Row.FindControl("lblCallTypeDesc");
            Literal lblCallStatusDesc = (Literal)e.Row.FindControl("lblCallStatusDesc");
            string leadID = DataBinder.Eval(e.Row.DataItem, "AgentLeadID").ToString();
            string callType = DataBinder.Eval(e.Row.DataItem, "CallType").ToString();
            string carID = DataBinder.Eval(e.Row.DataItem, "CarID").ToString();
            string customerID = DataBinder.Eval(e.Row.DataItem, "CustomerID").ToString();
            string callStatus = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
            hplCallNow.NavigateUrl = "offline/ViewCINoMatchFound.aspx?leadid=" + leadID + "&calltype=" + callType + "&carid=" + carID + "&custid=" + customerID;

            switch (callType)
            {
                case "100":
                    lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.FirstFollowUpCall + "</span>";
                    break;
                case "101":
                    lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallMeRequest + "</span>";
                    break;
                case "102":
                    lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallMeBack + "</span>";
                    break;
                case "103":
                    lblCallTypeDesc.Text = "<span style='color:black;'>" + Resources.General.CallDirect + "</span>";
                    break;
                default: lblCallTypeDesc.Text = "N/A"; break;
            }

            switch (callStatus)
            {
                case "1":
                    lblCallStatusDesc.Text = "<span style='color:red;'>" + Resources.General.Pending + "</span>";
                    break;
                case "2":
                    lblCallStatusDesc.Text = Resources.General.Callback + " <span style='color:red;'>[" + Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "CallBackDateTime").ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "]</span>";
                    break;
                case "3":
                    lblCallStatusDesc.Text = "<span style='color:green;'>" + Resources.General.FollowUp + "</span>";
                    break;
                default:
                    lblCallStatusDesc.Text = "N/A";
                    break;
            }
            
        }
    }
}
